package com.company;

import java.io.File;
import java.io.IOException;
import java.util.*;

/**
 * 最简单粗暴的实现
 */
public class Main {
    static String DEFAULT_ROOT_PATH = "/home/cyw/code/java/file-system/src/com/company/command";
    static String CHAR_MENU = "=====================";
    static List<String> filelist = new LinkedList<>();
    static Scanner scanner = new Scanner(System.in);
    public static void main(String[] args) throws IOException, InterruptedException {
        int command = 0;
       showMenu();
       while (true){
           System.out.println("请输入指令 ：");
           command = scanner.nextInt();
           parsingCommand(command);
       }
    }
    /**
     * 显示解析命令
     */
    public static void parsingCommand(int command) throws IOException, InterruptedException {
        File file = new File(DEFAULT_ROOT_PATH);
        File[] files =file.listFiles();
        if (command == 1){
            System.out.println(file.getAbsolutePath());

        }else if (command == 2){
            if (files.length == 0){
                return;
            }


            getFiles(DEFAULT_ROOT_PATH);


            Collections.sort(filelist, new Comparator<String>() {
                @Override
                public int compare(String s, String t1) {
                    if (s.length() > t1.length()){
                        String temp = s;
                        s = t1;
                        t1 = temp;
                    }
                    for (int i=3;i<s.length();i++){
                        if (s.charAt(i) != t1.charAt(i)){
                            return s.charAt(i) - t1.charAt(i);
                        }
                    }
                    return -1;
                }

            });
            for (int i=0;i<filelist.size();i++){
                System.out.println(filelist.get(i));
            }
            filelist.clear();
        }else if (command == 3){
            System.out.println("输入进入目录的绝对地址");
            Scanner scanner1 = new Scanner(System.in);
            String path = scanner1.nextLine();
            File newDir = new File(path);
            if (!newDir.exists()){
                System.out.println("地址");
            }else if (newDir.isFile()){
                System.out.println("这是一个文件");
            }else {
                DEFAULT_ROOT_PATH = path;
                System.out.println("当前目录： "+DEFAULT_ROOT_PATH);
            }
        }
    }

    static void getFiles(String filePath) {
        File root = new File(filePath);
        File[] files = root.listFiles();
        if (files.length == 0){
            return;
        }
        for (File file : files) {
            if (file.isDirectory()) {
                getFiles(file.getAbsolutePath());
                filelist.add("*: "+file.getPath());
            } else {
                filelist.add("@: "+file.getPath());
            }
        }
    }

    /**
     * 打印菜单
     */
    public static void showMenu(){
        System.out.println(CHAR_MENU+"菜单"+CHAR_MENU);
        System.out.println("1.当前目录路径");
        System.out.println("2.显示当前所有文件和目录");
        System.out.println("3.打开目录");
    }

}
